package bmatch.tech.todo.resource;

import bmatch.tech.todo.dao.BmAuthCodeMapper;
import bmatch.tech.todo.dao.BmUserMapper;
import bmatch.tech.todo.model.Result;
import bmatch.tech.todo.model.dataobject.BmUser;
import bmatch.tech.todo.service.AuthCodeService;
import bmatch.tech.todo.service.UserService;
import bmatch.tech.todo.util.FormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;

/**
 * @program:todo
 * @author:LinJinhao
 * @create:2019-01-23 17:43
 **/

@Component
@Path("/user")
public class UserResource {

	//用户服务
	@Autowired
	private UserService userService;
	@Autowired
	private BmAuthCodeMapper bmAuthCodeMapper;
	@Autowired
	private AuthCodeService authCodeService;
	@Autowired
	private BmUserMapper bmUserMapper;

	/**
	*   注册
	**/
	@POST
	@Path("register")
	@Produces(MediaType.APPLICATION_JSON)
	public Result register(@DefaultValue("") @QueryParam("email") String email,
	                         @DefaultValue("") @QueryParam("pwd") String pwd,
	                         @DefaultValue("") @QueryParam("second_pwd") String secondPwd){

		BmUser bmUser = new BmUser();
		Result result = new Result();

		//检查邮箱格式
		if (!FormatUtils.checkEmail(email) && StringUtils.isEmpty(email)){
			result.setErrorCode(Result.RESPONSE_FAIL);
			result.setSuccess(false);
			result.setErrorMessage("邮箱格式不正确，请重新输入");
		}

		//检查邮箱是否重复
		if (bmUserMapper.selectByEmail(email)!=null){
			result.setErrorCode(Result.RESPONSE_FAIL);
			result.setSuccess(false);
			result.setErrorMessage("邮箱重复，请重新输入");
		}

		//检查密码格式
		if (!FormatUtils.checkPwd(pwd)&&!FormatUtils.checkPwd(secondPwd)){
			result.setErrorCode(Result.RESPONSE_FAIL);
			result.setSuccess(false);
			result.setErrorMessage("密码必须包含6-18位的字母、数字以及其他符号");
		}

		//检查两次输入密码是否相同
		if (!pwd.equals(secondPwd)){
			result.setErrorCode(Result.RESPONSE_FAIL);
			result.setSuccess(false);
			result.setErrorMessage("两次输入的密码不相同，请重新输入");
		}

		bmUser.setEmail(email);
		bmUser.setPwd(pwd);

		userService.register(bmUser);

		authCodeService.generate(email);
		result.setErrorCode(Result.RESPONSE_SUCCESS);
		result.setSuccess(true);
		result.setErrorMessage("激活码已发送！");

		return result;
	}

	/**
	*  激活邮箱
	**/
	@GET
	@Path("activate")
	@Produces(MediaType.APPLICATION_JSON)
	public Result activate(@DefaultValue("") @QueryParam("code") String code){
		Result result = new Result();

		//检查激活码格式
		if (!FormatUtils.checkCode(code) && StringUtils.isEmpty(code)){
			result.setErrorCode(Result.RESPONSE_FAIL);
			result.setSuccess(false);
			result.setErrorMessage("激活码格式不正确，请重新输入");
		}

		//获取用户id
		String email = bmAuthCodeMapper.selectById(code).getUserId();
		authCodeService.activate(code);

		if (!userService.activate(email)){

		}
		result.setErrorCode(Result.RESPONSE_SUCCESS);
		result.setSuccess(true);
		result.setErrorMessage("账号激活成功!");
		return result;
	}
}
